<?php
/**
 * Name: SmsLogController.class.php
 * Description:
 * User: DIY
 * Date: 2016/9/24
 * Time: 18:12
 */

namespace Admin\Controller;


use Admin\Library\BootstrapPage;
use Common\Model\SmsLogModel;
use Common\Model\SmsTemplateModel;

class SmsLogController extends BaseController{

    /*
     * 短信记录
     * */
    public function index(){
        $SmsLogModel = new SmsLogModel();
        //搜索数据
        $data = array(
            '1'=>'1',
        );
        if(I('get.phone')!=''){
            $data['p1.phone'] = I('get.phone');
        }
        if(I('get.type')!=''){
            $data['p1.type'] = I('get.type');
        }
        if(I('get.status')!=''){
            $data['p1.status'] = I('get.status');
        }
        if(I('get.start_date')!=''){
            $data['p1.add_time'][] = array('lt',strtotime(I('get.start_date')));
        }
        if(I('get.end_date')!=''){
            $data['p1.add_time'][] = array('lt',strtotime(I('get.end_date')));
        }

        //取得模版列表
        $type_list = S('sms_type_list');
        if(empty($type_list)){
            $SmsTemplateModel = new SmsTemplateModel();
            $type_list = $SmsTemplateModel->field('title,type')->select();
            S('sms_type_list',$type_list);
        }
        $count = $SmsLogModel->alias('p1')->where($data)->count();
        $Page = new BootstrapPage($count,20);
        $list['page'] = $Page->show();
        $list['list'] = $SmsLogModel
            ->alias('p1')
            ->field('p1.*,p2.title')
            ->join('__SMS_TEMPLATE__ as p2 on p1.type=p2.type')
            ->where($data)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order('id desc')
            ->select();
        $this->assign('type_list',$type_list);
        $this->assign('list',$list);
        $this->display();
    }

    public function show($id){

        $SmsLogModel = new SmsLogModel();
        $sms_log = $SmsLogModel
            ->alias('p1')
            ->field('p1.*,p2.title')
            ->join('__SMS_TEMPLATE__ as p2 on p1.type=p2.type')
            ->where(array('p1.id'=>$id))
            ->find();
        $this->assign('sms_log',$sms_log);
        $this->display();
    }

    /*
     * 重新发送短信
     * */
    public function send(){

    }
}